package sql;

import hibernate.HibernateUtil;

import java.util.HashMap;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import dao.Client;
import dao.Dungeon;
import dao.Monster;
import dao.Room;
import dao.UserCharacter;

public class Call_DataBase implements DB_Interface {

	// ROOMS

	public void createRoom(Room r) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.save(r);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void updateRoom(Room r) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.update(r);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void deleteRoom(Room r) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.delete(r);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public List<Room> getAllRooms() throws HibernateException {
		Session session = HibernateUtil.currentSession();
		List list = session.find("from " + Room.class);
		return list;
	}

	public Room getRoom(int idroom) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Room r = (Room) session.load(Room.class, idroom);
		return r;
	}

	// MONSTER

	public void createMonster(Monster m) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.save(m);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void updateMonster(Monster m) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.update(m);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void deleteMonster(Monster m) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.delete(m);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public List<Monster> getAllMonsters() throws HibernateException {
		Session session = HibernateUtil.currentSession();
		List list = session.find("from " + Monster.class);
		return list;
	}

	public Monster getMonster(int idmonster) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Monster m = (Monster) session.load(Monster.class, idmonster);
		return m;
	}

	// USERCHARACTER

	public void createUserCharacter(UserCharacter uc) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.save(uc);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void updateUserCharacter(UserCharacter uc) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.update(uc);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void deleteUserCharacter(UserCharacter uc) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.delete(uc);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public List<UserCharacter> getAllUserCharacters() throws HibernateException {
		Session session = HibernateUtil.currentSession();
		List list = session.find("from " + UserCharacter.class);
		return list;
	}

	public UserCharacter getUserCharacter(String pseudouc) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		UserCharacter uc = (UserCharacter) session.load(UserCharacter.class,
				pseudouc);
		return uc;
	}

	// CLIENT

	public void createClient(Client c) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.save(c);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void updateClient(Client c) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.update(c);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public void deleteClient(Client c) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		session.delete(c);
		tx.commit();
		HibernateUtil.closeSession();
	}

	public List<Client> getAllClients() throws HibernateException {
		Session session = HibernateUtil.currentSession();
		List list = session.find("from " + Client.class);
		return list;
	}

	public Client getClient(String logincli) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Client c = (Client) session.load(Client.class, logincli);
		return c;
	}

	@Override
	public void createAllRooms(List<Room> roomlist) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clearRoom() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createAllMonsters(List<Monster> monsterlist) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clearMonster() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createAllUserCharacters(List<UserCharacter> uclist)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clearUserCharacter() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createAllClients(List<Client> clientlist) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clearClient() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createAllLinks(List<Room> listroom) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setAllLinks(HashMap<Integer, Room> hmrooms) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clearLinkRooms() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Room> getAllRooms(Dungeon myDungeon) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
